package com.pubinfo.passbook;

import com.github.xiaoymin.knife4j.discovery.annotation.EnableKnife4jCloudDiscovery;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import javax.annotation.Resource;
import java.util.TimeZone;

/**
 * Hello world!
 */
@Slf4j
@MapperScan("com.pubinfo.passbook.common.mapper")
@SpringBootApplication
@EnableKnife4jCloudDiscovery
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SyncDataApplication {
  
  public static void main(String[] args) {
    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
    ApplicationContext context = SpringApplication.run(SyncDataApplication.class, args);
    //手动控制 rabbitmq是否启动
    RabbitMQStart rabbitMQRun = context.getBean(RabbitMQStart.class);
    rabbitMQRun.start();
  }
  
  @Bean
  public RabbitMQStart rabbitMQRun() {
    return new RabbitMQStart();
  }
  
  private static class RabbitMQStart {
    
    @Value("${spring.rabbitmq.enable}")
    private Boolean rabbitmqStart;
    
    @Resource
    RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
    
    public void start() {
      if (rabbitmqStart) {
        rabbitListenerEndpointRegistry.start();
      } else {
        rabbitListenerEndpointRegistry.stop();
      }
      log.info(" Rabbitmq:" + rabbitmqStart + "===================");
    }
  }
}
